package Servlet;

import Bean.Employee;
import Bean.User;
import Utils.IOUtils;
import Utils.JsonToObjUtils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/login")
//这里服务层
public class ServerletLogin extends HttpServlet {
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
		doPost(req,resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		req.setCharacterEncoding("UTF-8");
		resp.setContentType("application/json;charset=UTF-8");
		resp.setHeader("Access-Control-Allow-Origin","*");
		String username1="root";
		String password1="maihao1122";
		String jsonStr= IOUtils.readStr(req.getInputStream());
		User user= JsonToObjUtils.jsonStrToObj(jsonStr, User.class);
		String jsonStr1=JSONObject.toJSONString(user);
		if(user.getPassword().equals(password1) ) {
			if(user.getUsername().equals(username1)) {
				resp.getWriter().write("{"+user.getUsername()+" "+user.getPassword()+" "+"msg:success}");
			}
		}
		resp.getWriter().write("{"+user.getUsername()+" "+user.getPassword()+" "+"msg:failed}");

	}
}
